classSolution(object): defintersection(self, nums1, nums2): """ :type nums1: List[int] :type nums2: List[int] :rtype: List[int] """ # 方法1:直接用取交集的方法 # 方法2:是使用map实现取交集 result = [] mapper = dict() for x in nums1: mapper[x] = 1 for y in nums2: if mapper.get(y) and mapper.get(y) == 1: result.append(y) mapper[y] = 0 return result
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
funcintersection(nums1 []int, nums2 []int) []int { var mapper = make(map[int]int) var result []int
for _, x:= range nums1{ mapper[x] = 1 }
for _, y:= range nums2{ i, ok := mapper[y] if ok && i == 1{ result = append(result, y) mapper[y] = 0 } }